package com.wyth.lottery.fragment;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wyth.lottery.R;
import com.wyth.lottery.activity.ShoppingActivity;
import com.wyth.lottery.base.BaseFragment;
import com.wyth.lottery.base.SaveUtils;
import com.wyth.lottery.bean.SSQDataBean;
import com.wyth.lottery.bean.SSQDataDTBean;
import com.wyth.lottery.bean.SSQSelectBean;
import com.wyth.lottery.utils.MathC;
import com.wyth.lottery.utils.MyApplication;
import com.wyth.lottery.utils.Number;
import com.wyth.lottery.utils.RandomUtils;
import com.wyth.lottery.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by phh on 2017/10/31.
 * 双色球胆拖界面
 */

public class DTFragment extends BaseFragment {
    @BindView(R.id.rv_red_dm)
    RecyclerView mRvRedDm;
    @BindView(R.id.rv_red_tm)
    RecyclerView mRvRedTm;
    @BindView(R.id.rv_blue)
    RecyclerView mRvBlue;
    @BindView(R.id.tv_number_bei)
    TextView mTvNumberBei;
    @BindView(R.id.tv_money_number)
    TextView mTvMoneyNumber;
    @BindView(R.id.tv_money)
    TextView mTvMoney;
    @BindView(R.id.bonus_status)
    TextView mBonusStatus;
    @BindView(R.id.duplex_time)
    TextView mDuplexTime;
    Unbinder unbinder;
    private ArrayList<SSQSelectBean> TMRed = new ArrayList<>();
    private ArrayList<SSQSelectBean> DMRed = new ArrayList<>();
    private ArrayList<SSQSelectBean> blue = new ArrayList<>();
    //用户所选胆码集合
    private List<Integer> mTMList = new ArrayList<>();
    //所选胆码集合
    private List<Integer> mDMList = new ArrayList<>();


    //判断用户是否从购物车回来的
    private int sign = 0;
    //分别用来统计用户选择个的个数
    private int mRedDm = 0;
    private int mRedTm = 0;
    private int mBlueNumber = 0;
    /**
     * 用户选择的号码一共有多少注
     */
    private int mNumber = 0;
    private BlueAdapter mBlueAdapter;
    private TMRedAdapter mTMAdapter;
    private DMRedAdapter mDMAdapter;
    private List<SSQSelectBean> mMblue;
    private List<SSQSelectBean> mRed;
    //TODO 这是服务器获取的限制钱数数据,需要赋值
    private int mGetMaxMoney;

    @Override
    protected View getSuccessView() {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.fragment_ssq_dt, null);
        return view;
    }

    @Override
    protected void initDate() {
        stateLayout.showSuccessView();
        initRedDM();
        initRedTM();
        initBlue();
        EventBus.getDefault().register(this);
        SSQDataDTBean stickyEvent = EventBus.getDefault().getStickyEvent(SSQDataDTBean.class);
        if (stickyEvent != null) {
            EventBus.getDefault().removeStickyEvent(stickyEvent);
        }
        mBonusStatus.setText(SaveUtils.getGoodsInfos().mBonusStatus);
        mDuplexTime.setText(SaveUtils.getGoodsInfos().mDuplexTime);
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Object event) {
        if (event instanceof SSQDataDTBean) {
            sign = 1;
            SetTime((SSQDataDTBean) event);
        }
    }


    private void SetTime(SSQDataDTBean event) {
        mMblue = event.getMblue();
        mRed = event.getRed();
        for (SSQSelectBean ssqSelectBean : mMblue) {
            boolean selectBoolean = ssqSelectBean.selectBoolean;
            boolean isBoolean = ssqSelectBean.isBoolean;
            String text = ssqSelectBean.getText();

            if (selectBoolean == false && isBoolean == true) {
                for (SSQSelectBean selectBean : blue) {
                    if (text.equals(selectBean.getText())) {
                        selectBean.selectBoolean = false;
                        selectBean.isBoolean = true;
                        mBlueNumber++;
                    }
                }
            }

        }

        //红球胆码
        for (SSQSelectBean ssqSelectBean : mRed) {
            boolean isBoolean = ssqSelectBean.isBoolean;
            boolean selectBoolean = ssqSelectBean.selectBoolean;
            String text = ssqSelectBean.getText();
            if (selectBoolean == true && isBoolean == true) {
                for (SSQSelectBean selectBean : DMRed) {
                    if (text.equals(selectBean.getText())) {
                        selectBean.selectBoolean = false;
                        selectBean.isBoolean = true;
                        mRedDm++;
                    }
                    for (SSQSelectBean selectBean2 : TMRed) {
                        if (text.equals(selectBean2.getText())) {
                            selectBean2.isBoolean = false;

                        }
                    }
                    mTMAdapter.setNewData(TMRed);
                }

            } else if (selectBoolean == true && isBoolean == false) {
                for (SSQSelectBean selectBean : TMRed) {
                    if (text.equals(selectBean.getText())) {
                        selectBean.selectBoolean = false;
                        selectBean.isBoolean = true;
                        mRedTm++;
                    }
                    for (SSQSelectBean selectBean2 : DMRed) {
                        if (text.equals(selectBean2.getText())) {
                            selectBean2.isBoolean = false;

                        }
                    }
                    mDMAdapter.setNewData(DMRed);
                }
            }

        }

        mBlueAdapter.setNewData(blue);
        mDMAdapter.setNewData(DMRed);
        mTMAdapter.setNewData(TMRed);

        int i = MathC.numberDT(mRedDm, mRedTm, mBlueNumber);
        int money = event.getMoney();
        mTvNumberBei.setText(String.valueOf(money / 2 / i));
        setText();
    }

    private void initBlue() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mRvBlue.setLayoutManager(staggeredGridLayoutManager);
        Number.getList(16, blue);
        mBlueAdapter = new BlueAdapter(blue);
        mRvBlue.setAdapter(mBlueAdapter);
    }

    private void initRedTM() {
        if (!mTMList.isEmpty()) {
            mTMList.clear();
        }
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
        mRvRedTm.setLayoutManager(staggeredGridLayoutManager);
        Number.getList(33, TMRed);
        mTMAdapter = new TMRedAdapter(TMRed);
        mDMAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SSQSelectBean bean = TMRed.get(position);
                if (bean.isClick()) {
                    if (bean.isSelectBoolean()) {
                        mTMList.add(Integer.valueOf(bean.getText().toString()));
                        TMRed.get(position).setSelectBoolean(true);
                        DMRed.get(position).setClick(false);
                    } else {
                        mTMList.add(Integer.valueOf(bean.getText().toString()));
                        TMRed.get(position).setSelectBoolean(false);
                        DMRed.get(position).setClick(true);
                    }
                    mDMAdapter.notifyDataSetChanged();
                    mTMAdapter.notifyDataSetChanged();
                }
            }
        });

        mRvRedTm.setAdapter(mTMAdapter);

    }

    private void initRedDM() {
        if (!mDMList.isEmpty()) {
            mDMList.clear();
        }
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
        mRvRedDm.setLayoutManager(staggeredGridLayoutManager);
        Number.getList(33, DMRed);
        mDMAdapter = new DMRedAdapter(DMRed);

        mDMAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SSQSelectBean bean = DMRed.get(position);
                if (bean.isClick()) {
                    if (mDMList.size() >= 5) {
                        ToastUtil.makeToast("胆码最多选5个", 0);
                    } else {
                        //isSelectBoolean  false未选中
                        if (!bean.isSelectBoolean()) {
                            mDMList.add(Integer.valueOf(bean.getText()));
                            DMRed.get(position).setSelectBoolean(true);
                            TMRed.get(position).setClick(false);
                        } else {
                            mDMList.remove(Integer.valueOf(bean.getText()));
                            DMRed.get(position).setSelectBoolean(false);
                            TMRed.get(position).setClick(true);
                        }
                        mDMAdapter.notifyDataSetChanged();
                        mTMAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

        mRvRedDm.setAdapter(mDMAdapter);
    }


    @OnClick({R.id.tv_next, R.id.iv_return, R.id.zw_mai, R.id.tv_number_bei, R.id.zw_tv_bei})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                setNext();
                break;
            case R.id.iv_return:
                if (sign == 0) {
                    getActivity().finish();
                } else if (sign == 1) {
                    SaveUtils.getGoodsInfos().mlist.add(0, new SSQDataBean(mRed, mMblue, mNumber, "胆拖投注", mNumber * 2));
                    SaveUtils.getGoodsInfos().mTvMoney += (mNumber * 2);
                    SaveUtils.getGoodsInfos().mTvMoneyNumber += mNumber;
                    Intent intent = new Intent(MyApplication.getContext(), ShoppingActivity.class);
                    intent.putExtra("before", "before ");
                    startActivity(intent);
                }

                break;
            case R.id.zw_mai:
                initDialog();
                break;
            case R.id.tv_number_bei:
                initDialog();
                break;
            case R.id.zw_tv_bei:
                initDialog();
                break;
        }
    }

    /**
     * 卖多少倍的dialog
     */
    private void initDialog() {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.dalog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final AlertDialog alertDialog = builder.setView(view).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        WindowManager windowManager = getActivity().getWindowManager();

        Display defaultDisplay = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.height = (int) (defaultDisplay.getHeight() * 0.9);
        attributes.width = (int) (defaultDisplay.getWidth() * 0.6);
        window.setAttributes(attributes);


        final TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
        TextView tv_number1 = (TextView) view.findViewById(R.id.tv_number1);
        TextView tv_number2 = (TextView) view.findViewById(R.id.tv_number2);
        TextView tv_number3 = (TextView) view.findViewById(R.id.tv_number3);
        TextView tv_number4 = (TextView) view.findViewById(R.id.tv_number4);
        TextView tv_number5 = (TextView) view.findViewById(R.id.tv_number5);
        TextView tv_number6 = (TextView) view.findViewById(R.id.tv_number6);
        TextView tv_number7 = (TextView) view.findViewById(R.id.tv_number7);
        TextView tv_number8 = (TextView) view.findViewById(R.id.tv_number8);
        TextView tv_number9 = (TextView) view.findViewById(R.id.tv_number9);
        TextView tv_number0 = (TextView) view.findViewById(R.id.tv_number0);
        TextView tv_delete = (TextView) view.findViewById(R.id.tv_delete);
        TextView tv_false = (TextView) view.findViewById(R.id.tv_false);
        TextView tv_true = (TextView) view.findViewById(R.id.tv_true);

        final List<String> mlist = new ArrayList<>();

        RandomUtils.SetOnClickListener(tv_number1, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number2, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number3, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number4, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number5, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number6, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number7, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number8, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number9, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number0, mlist, tv_number);

        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_number.setText(String.valueOf(1));
                mlist.clear();
            }
        });

        tv_false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        tv_true.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mlist.size() == 0) {
                    mlist.add(String.valueOf(1));
                    RandomUtils.getString(mlist, mTvNumberBei);
                    alertDialog.dismiss();
                } else {
                    RandomUtils.getString(mlist, mTvNumberBei);
                    alertDialog.dismiss();
                    setText();
                }
            }
        });

    }

    /**
     * 下一步 跳转到购物车列表
     */
    private void setNext() {
//        if (mRedTm >= 2 && mNumber >= 2 && mBlueNumber >= 1) {
        if ((mDMList.size() + mTMList.size()) >= 7 && mBlueNumber >= 1) {
            List<SSQSelectBean> mRed = new ArrayList<>();
            List<SSQSelectBean> mblue = new ArrayList<>();

            for (SSQSelectBean ssqSelectBean : DMRed) {
                boolean selectBoolean = ssqSelectBean.selectBoolean;
                if (selectBoolean) {
                } else {
                    mRed.add(new SSQSelectBean(ssqSelectBean.getText(), true, true));
                }
            }
            for (SSQSelectBean ssqSelectBean : TMRed) {
                boolean selectBoolean = ssqSelectBean.selectBoolean;
                if (selectBoolean) {
                } else {
                    mRed.add(new SSQSelectBean(ssqSelectBean.getText(), true, false));
                }
            }
            for (SSQSelectBean ssqSelectBean : blue) {
                boolean selectBoolean = ssqSelectBean.selectBoolean;
                if (selectBoolean) {
                } else {
                    mblue.add(new SSQSelectBean(ssqSelectBean.getText(), false, true));
                }
            }
            int i = MathC.numberDT(mRedDm, mRedTm, mBlueNumber);
            int money = i * 2;
            //测试1000
            //mGetMaxMoney = 1000;
            if (money >= mGetMaxMoney) {
                ToastUtil.makeToast("单次投注不能超" + mGetMaxMoney, 0);
                return;
            }
            EventBus.getDefault().postSticky(new SSQDataBean(mRed, mblue, mNumber, 1, "胆拖投注", money));

            startActivity(new Intent(MyApplication.getContext(), ShoppingActivity.class));
            getActivity().finish();
        } else {
            ToastUtil.makeToast("脱码的个数不能小于2,请选择完整的一注", 0);
        }
    }


    /**
     * 红色球胆码的适配器
     */
    private class DMRedAdapter extends BaseQuickAdapter<SSQSelectBean, BaseViewHolder> {


        public DMRedAdapter(ArrayList<SSQSelectBean> data) {
            super(R.layout.ssq_red_item, data);
        }


        @Override
        protected void convert(final BaseViewHolder helper, final SSQSelectBean item) {
            final TextView textView = helper.getView(R.id.t);
            helper.setText(R.id.t, item.text);
            if (item.isClick()) {
                if (!item.isSelectBoolean()) {
                    textView.setBackgroundResource(R.mipmap.img_red_false);
                    textView.setTextColor(getResources().getColor(R.color.red));
                } else {
                    textView.setBackgroundResource(R.mipmap.img_red_true);
                    textView.setTextColor(getResources().getColor(R.color.white));
                }

            } else {
                textView.setBackgroundResource(R.mipmap.img_gray_backgroup);
                textView.setTextColor(getResources().getColor(R.color.white));
                textView.setEnabled(false);
            }

//            helper.setOnClickListener(R.id.t, new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (item.selectBoolean) {
//                        if (mRedDm < 5) {
//                            textView.setBackgroundResource(R.mipmap.img_red_true);
//                            textView.setTextColor(getResources().getColor(R.color.white));
//                            item.selectBoolean = false;
//                            mRedDm++;
//                            setText();
//                            String text = item.getText();
//                            for (SSQSelectBean ssqSelectBean : TMRed) {
//                                if (text.equals(ssqSelectBean.getText())) {
//                                    ssqSelectBean.isBoolean = false;
//                                }
//                            }
//                            mTMAdapter.setNewData(TMRed);
//                        } else {
//                            ToastUtil.makeToast("最多只能选择5个", 0);
//                        }
//                    } else {
//
//                        textView.setBackgroundResource(R.mipmap.img_red_false);
//                        textView.setTextColor(getResources().getColor(R.color.red));
//                        item.selectBoolean = true;
//                        mRedDm--;
//                        setText();
//
//                        String text = item.getText();
//                        for (SSQSelectBean ssqSelectBean : TMRed) {
//                            if (text.equals(ssqSelectBean.getText())) {
//                                ssqSelectBean.isBoolean = true;
//                            }
//                        }
//                        mTMAdapter.setNewData(TMRed);
//                    }
//
//                }
//            });
        }
    }

    /**
     * 给当前界面设置用户选择了多少注
     */
    private void setText() {
        int i = MathC.numberDT(mRedDm, mRedTm, mBlueNumber);
        mNumber = i;
        mTvMoneyNumber.setText(String.valueOf(i));
        mTvMoney.setText(String.valueOf(i * 2));


    }

    /**
     * 红色球拖码的适配器
     */
    private class TMRedAdapter extends BaseQuickAdapter<SSQSelectBean, BaseViewHolder> {


        public TMRedAdapter(ArrayList<SSQSelectBean> data) {
            super(R.layout.ssq_red_item, data);
        }


        @Override
        protected void convert(final BaseViewHolder helper, final SSQSelectBean item) {
            final TextView textView = helper.getView(R.id.t);

            helper.setText(R.id.t, item.text);
            if (item.isClick()) {
                if (!item.isSelectBoolean()) {
                    textView.setBackgroundResource(R.mipmap.img_yellow_two_backgroup);
                    textView.setTextColor(getResources().getColor(R.color.yelloww));
                } else {
                    textView.setBackgroundResource(R.mipmap.img_yellow_backgroup);
                    textView.setTextColor(getResources().getColor(R.color.white));
                }
            } else {
                textView.setBackgroundResource(R.mipmap.img_gray_backgroup);
                textView.setTextColor(getResources().getColor(R.color.white));
                textView.setEnabled(false);
            }


//            helper.setOnClickListener(R.id.t, new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (item.selectBoolean) {
//                        textView.setBackgroundResource(R.mipmap.img_yellow_backgroup);
//                        textView.setTextColor(getResources().getColor(R.color.white));
//                        item.selectBoolean = false;
//                        mRedTm++;
//                        setText();
//
//                        String text = item.getText();
//                        for (SSQSelectBean ssqSelectBean : DMRed) {
//                            if (text.equals(ssqSelectBean.getText())) {
//                                ssqSelectBean.isBoolean = false;
//                            }
//                        }
//                        mDMAdapter.setNewData(DMRed);
//                    } else {
//                        textView.setBackgroundResource(R.mipmap.img_yellow_two_backgroup);
//                        textView.setTextColor(getResources().getColor(R.color.yelloww));
//                        item.selectBoolean = true;
//                        mRedTm--;
//                        setText();
//                        String text = item.getText();
//                        for (SSQSelectBean ssqSelectBean : DMRed) {
//                            if (text.equals(ssqSelectBean.getText())) {
//                                ssqSelectBean.isBoolean = true;
//                            }
//                        }
//                        mDMAdapter.setNewData(DMRed);
//                    }
//
//                }
//            });
        }
    }

    /**
     * 蓝色球脱码的适配器
     */
    private class BlueAdapter extends BaseQuickAdapter<SSQSelectBean, BaseViewHolder> {


        public BlueAdapter(ArrayList<SSQSelectBean> item) {
            super(R.layout.ssq_blue_item, item);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final SSQSelectBean item) {
            final TextView textView = helper.getView(R.id.tt);

            helper.setText(R.id.tt, item.text);

            if (item.selectBoolean) {
                textView.setBackgroundResource(R.mipmap.img_blue_false);
                textView.setTextColor(getResources().getColor(R.color.ssqblue));
            } else {
                textView.setBackgroundResource(R.mipmap.img_blue_true);
                textView.setTextColor(getResources().getColor(R.color.white));
            }


            helper.setOnClickListener(R.id.tt, new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (item.selectBoolean) {
                        textView.setBackgroundResource(R.mipmap.img_blue_true);
                        textView.setTextColor(getResources().getColor(R.color.white));
                        item.selectBoolean = false;
                        mBlueNumber++;
                        setText();
                    } else {
                        textView.setBackgroundResource(R.mipmap.img_blue_false);
                        textView.setTextColor(getResources().getColor(R.color.ssqblue));
                        item.selectBoolean = true;
                        mBlueNumber--;
                        setText();
                    }

                }

            });


        }
    }
}
